package com.hrpmar.service.impl;

import com.hrpmar.controller.vo.Node;
import com.hrpmar.mapper.MenuMapper;
import com.hrpmar.mapper.PermissionMapper;
import com.hrpmar.mapper.RoleMapper;
import com.hrpmar.pojo.Menu;
import com.hrpmar.pojo.Role;
import com.hrpmar.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by c on 2017-11-14.
 */
@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    public MenuMapper menuMapper;

    @Override
    public Map<Menu, List<Menu>> searchMenus(Integer roleId) {
        Map<Menu, List<Menu>> menuMap = new TreeMap<Menu, List<Menu>>();
        //获取角色所属的部门编号
        Role role= roleMapper.getRoleByRoleId(roleId);
        Integer deId = role.getDeId();
        //获取部门编号所属的菜单编号
        List<Integer> menus = permissionMapper.getMenuIdByDeId(deId);
        if(menus!=null && menus.size()>0){
            for(Integer menuId:menus){
                //根据菜单编号获取菜单信息
                Menu menu01=menuMapper.getMenuById(menuId);
                if(menu01.getPid()==null){
                    //创建二级菜单list
                    List<Menu> menus02 = new ArrayList<Menu>();
                    menuMap.put(menu01,menus02);
                    for(Integer menuId2:menus){
                        Menu menu02 = menuMapper.getMenuById(menuId2);
                        if(menu02.getPid()==menu01.getMenuId()){
                            menus02.add(menu02);
                        }
                    }
                }
            }
        }

        return menuMap;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Node> searchNodes4Tree(Integer Id) {
        List<Integer> menuList=permissionMapper.getMenuIdByDeId(Id);
        List<Node> nodes01=new ArrayList<Node>();
        List<Menu> menus01=menuMapper.searchLevel01();
        for(Menu menu01:menus01){
            Node node01=new Node();
            node01.setIcon(menu01.getIconSkin());
            node01.setId(menu01.getMenuId());
            node01.setText(menu01.getName());
            if(menuList.contains(node01.getId())){
                node01.getState().setChecked(true);
            }
            List<Menu> menus02=menu01.getChildren();
            if(menus02!=null&&menus02.size()>0){
                for(Menu menu02:menus02){
                    Node node02=new Node();
                    node02.setIcon(menu02.getIconSkin());
                    node02.setId(menu02.getMenuId());
                    node02.setText(menu02.getName());
                    node02.setNodes(null);
                    if(menuList.contains(node02.getId())){
                        node02.getState().setChecked(true);
                    }
                    node01.getNodes().add(node02);
                }
            }
            nodes01.add(node01);
        }
        return nodes01;
    }

    @Override
    public void grant(Integer roleId, Integer[] menuIds) {
        permissionMapper.deleteByRoleId(roleId);
        if(menuIds!=null&&menuIds.length>0){
            for(Integer menuId:menuIds){
                permissionMapper.add(roleId,menuId);
            }
        }
    }
}
